<?php
/**
 * 获取订单数据写入数据库
 * Created by wzb.
 * User: wzb
 * createtime:2017/08/12
 * updatetime:2017/08/22
 */


require("interface.php");

$app_info = "select * from dlz_account_info where thirdname='mall003'";
$query = $db->query($app_info);//返回影响了多少行数据
echo "***********Get account info********************\n";
while($row = $query->fetch()){
        $id_m = $row['id'];
        $store_name_m = $row['store_name'];
        $token_m = $row['token'];
        $baseUrl_m = $row['baseUrl'];
        $username_m = $row['username'];
        $password_m = $row['password'];
        echo "//////////////$store_name_m/////////////////////\n";
        //print_r($row);
        //$Mazentop = new Mazentop($token_m,$baseUrl_m,$username_m,$password_m);
        echo "-------------------Start  get sku------------------------\n";
        $item_info = "select * from dlz_attribute_all_id where increment_id='X000001743'";
        $query_item = $db->query($item_info);//返回影响了多少行数据
        while($rows = $query_item->fetch()){
          $sku = $rows['sku'];
          $store_id = $rows['store_id'];
          $options_id = $rows['options_id'];
          $increment_id = $rows['increment_id'];
          $pice_or_lotnums = $rows['pice_or_lotnums'];
          $item_id = $rows['item_id'];
          $item_price = $rows['price'];

          echo "\n..................get sku: $sku....$item_id..........................\n";
          Getpruductdetial($db,$sku,$options_id,$increment_id,$pice_or_lotnums,$store_id,$item_id,$item_price);
        }

}//第一个while

function Getpruductdetial($db,$sku,$options_id,$increment_id,$pice_or_lotnums,$store_id,$item_id,$item_price){
      //定义数组
      $zuzhuangarrary = array();
      $zuzhuangarrary['increment_id'] = $increment_id;
     // $zuzhuangarrary['amount'] = $item_price;//子订单金额
      //通过组装的产品标示 storeid_sku 获取产品 
      $storeid_sku = $store_id.'-'.$sku;
      echo "||||||||||||||||||||||||---from localhost sync data---|||||||||||||||||||||||\n";
     $product_info = "select * from dlz_product_attr_$store_id where storeid_sku='$storeid_sku'";
      // $product_info = "select * from dlz_product_attr where storeid_sku='$storeid_sku'";
      $query_product = $db->query($product_info);//返回影响了多少行数据
      while($rows_product = $query_product->fetch()){
            $zuzhuangarrary['product_id'] = $rows_product['product_id'];//产品id
            $category = $rows_product['attribute_set_id'];//类目
            //$zuzhuangarrary['category'] = $category;
            $price = $rows_product['price'];//产品基价
            //$amount = "$item_price" - "$price";
            $zuzhuangarrary['amount'] = $price;//子订单金额
            $zuzhuangarrary['productCount'] = $pice_or_lotnums;//数量
            $zuzhuangarrary['status'] = $rows_product['status'];//产品状态
            $zuzhuangarrary['created_at'] = $rows_product['created_at'];//产品上传时间
            $zuzhuangarrary['updated_at'] = $rows_product['updated_at'];//产品修改时间
            $zuzhuangarrary['weight'] = $rows_product['weight'];//产品重量
            $zuzhuangarrary['color'] = $rows_product['color'];//颜色 
            $zuzhuangarrary['grade'] = $rows_product['grade'];//等级
            $zuzhuangarrary['texture'] = $rows_product['texture'];//花型
            $zuzhuangarrary['laceSize'] = $rows_product['lace_size'];//lace尺寸
            //$zuzhuangarrary['density'] = $rows_product['density'];//密度
            $zuzhuangarrary['capSize'] = $rows_product['size'];//大小
            $zuzhuangarrary['memo'] = $rows_product['wig_type'];//备注
            $items_package = $rows_product['items_package'];//打包方式
            $country_type_value = $rows_product['country_type'];//国家

            /***重要的地方尺寸处理***/
            //WIG  Stretched_inc_option_id
                  //解析json key=option_id  value=option_value
          $option_arr = json_decode($options_id);
          $firstproduct = array();
          foreach ($option_arr as $optionkey => $optionvalue) {
            //产品一
            
            $Closure = $rows_product['Closure_inch_option_id'];
            $Stretched = $rows_product['Stretched_inc_option_id'];
            $part_design = $rows_product['part_design_option_id'];
            $country_type = $rows_product['country_type_option_id'];
            $density = $rows_product['density_option_id'];//2017/11/1
            $storeid_sku_value = $store_id.'-'.$sku.'-'.$optionvalue;
            echo "<         $storeid_sku_value        >\n";
            //判定订单产品中的option 与  已同步产品中的option ，存在则找到相关信息
            if ($Closure == $optionkey) {
              $firstproduct['P1']['closure'] = getoptionvalue($table_="dlz_product_closureinch",$storeid_sku_value,$db);
              //$lotNum = '1';
              $firstproduct['P1']['lotNum'] = '1';//打包条数
            }elseif ($Stretched == $optionkey) {
              $firstproduct['P1']['Stretched'] = getoptionvalue($table_="dlz_product_stretchedinc",$storeid_sku_value,$db);
             //if(strpos($items_package,'Pack') !== false){
               // if(strpos($items_package,'3') !== false){
               //   $lotNum = '3';
               // }elseif (strpos($items_package,'4') !== false) {
               //   $lotNum = '4';
               // }
             // }else{
               // $lotNum = '1';
             // }

              $firstproduct['P1']['lotNum'] = '1';//打包条数
            }elseif ($part_design == $optionkey) {
              $firstproduct['P1']['hairDesign'] = getoptionvalue($table_="dlz_product_partdesign",$storeid_sku_value,$db);
            }elseif ($country_type == $optionkey && (!empty($Closure) || !empty($Stretched))) {
              $firstproduct['P1']['type_country'] = getoptionvalue($table_="dlz_product_countrytype",$storeid_sku_value,$db);

            }elseif($density == $optionkey){//2017/11/1
              $firstproduct['P1']['density'] = getoptionvalue($table_="dlz_product_density",$storeid_sku_value,$db);
              /**密度变为可选项 功能过渡判断*/
              if(empty($firstproduct['P1']['density']) || $firstproduct['P1']['density'] == ''){
                $firstproduct['P1']['density'] = $rows_product['density'];//密度
              }

              

            }
           
          }
           insertsql($zuzhuangarrary,$get_param=$firstproduct,$category,$db,$country_type_value,$item_id);
          $secondproduct = array();
          foreach ($option_arr as $optionkey2 => $optionvalue2) {
            //产品二
            $storeid_sku_value = $store_id.'-'.$sku.'-'.$optionvalue2;
            echo "<         $storeid_sku_value        >\n";
            $Weft = $rows_product['Weft_inc_option_id'];
            $country_type = $rows_product['country_type_option_id'];
            if($Weft == $optionkey2){
              $secondproduct['P2']['weaving'] = getoptionvalue($table_="dlz_product_weftinc",$storeid_sku_value,$db);
              $lotNum = '1';
              $secondproduct['P2']['lotNum'] = $lotNum;//打包条数
            }elseif($country_type == $optionkey2 && !empty($Weft)) {
              $secondproduct['P2']['type_country'] = getoptionvalue($table_="dlz_product_countrytype",$storeid_sku_value,$db);

            }

             
          }
           insertsql($zuzhuangarrary,$get_param=$secondproduct,$category,$db,$country_type_value,$item_id);
      }

 echo "++++++++++++++++++end++++++++++++++++++++\n"  ; 
}



function getoptionvalue($table_,$storeid_sku_value,$db){
    $sql_optionvalue = "select * from $table_ where storeid_sku='$storeid_sku_value'";
    $query_optionvalue = $db->query($sql_optionvalue);//返回影响了多少行数据
      while($rows_optionvalue = $query_optionvalue->fetch()){
            $optionvalue_title = $rows_optionvalue['title'];
      }
      return $optionvalue_title;
}


function insertsql($zuzhuangarrary,$get_param,$category,$db,$country_type_value,$item_id){
   // $zuzhuangarrary['P2']['weaving']
   
   if(isset($get_param['P1'])){
    $zuzhuangarrary['child_id'] = $zuzhuangarrary['increment_id'].'-'.$item_id.'-'.'01';
    $zuzhuangarrary['orderit_itemid'] = $zuzhuangarrary['increment_id'].'-'.$item_id.'-'.'01';
    $type_lacesize = $zuzhuangarrary['laceSize'];
    foreach($get_param['P1'] as $key_p => $value_p){
           //处理类目，类型 ,蕾丝
          if($key_p == "closure" || $key_p == "Stretched"){
            $zuzhuangarrary['inch'] = trim($value_p);
          }else{
            $zuzhuangarrary[$key_p] = trim($value_p);
          }
          if($type_lacesize == "Full" || $type_lacesize == "Full Lace Frontal Wig"){
            $zuzhuangarrary['laceSize'] = 'Full lace';
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "Full Lace Wigs";
          }elseif($category == "Hair Weft with Closure" && $type_lacesize == "360"){
            $zuzhuangarrary['category'] = "Lace Front";
            $zuzhuangarrary['type'] = "360 Lace Frontal";
          }elseif($category == "Wig" && $type_lacesize == "360" || $type_lacesize =="360 Lace Frontal Wig"){
            $zuzhuangarrary['laceWigType'] = "360 lace wig"; 
            $zuzhuangarrary['category'] = "Wigs";
          }elseif($type_lacesize == "4\" x 4\""){
            $zuzhuangarrary['category'] = "Closure";
            $zuzhuangarrary['type'] = "Lace Closure";
          }elseif($type_lacesize == "4\" x 13\""){
            $zuzhuangarrary['category'] = "Lace Front";
            $zuzhuangarrary['type'] = "Lace Front";
          }elseif(empty($type_lacesize)){
            $zuzhuangarrary['category'] = "Hair Weaving";
          }elseif ($type_lacesize == "3\" x 12\""){
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "Lace Front wigs";
          }elseif ($type_lacesize == "6\" x 12\"") {
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "6*12 lace wig";
          }elseif ($category == "Hair Closure" && $type_lacesize == "360" || trim($type_lacesize) == "360 Lace Frontal") {
            $zuzhuangarrary['category'] = "Lace Front";
            $zuzhuangarrary['type'] = "360 Lace Frontal";
          }elseif ($type_lacesize == "6\" x 12\" bob") {
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "6*12 lace bob wig";
          }
          if(empty($zuzhuangarrary['type_country'])){
              $zuzhuangarrary['type_country'] = $country_type_value;
          }

    }
    //print_r($zuzhuangarrary);
    echo "\n";
    $result = Getinsert($arrData=$zuzhuangarrary,$table='dlz_order_product',$db,$sync_data="0");
    //给定处理过的产品，用 1 标示；
    if($result == '0'){
          $syncstatus_sql = "update dlz_attribute_all_id set sync_status='1' where item_id='$item_id'";
          $db->exec($syncstatus_sql);
    }

    //
   }elseif (isset($get_param['P2'])) {
    $zuzhuangarrary['child_id'] = $zuzhuangarrary['increment_id'].'-'.$item_id.'-'.'02';
    $zuzhuangarrary['orderit_itemid'] = $zuzhuangarrary['increment_id'].'-'.$item_id.'-'.'02';
         foreach($get_param['P2'] as $key_p1 => $value_p1){
          echo "$key_p1******\n";
          if($key_p1 == "weaving"){
            $zuzhuangarrary['inch'] = trim($value_p1);
            $zuzhuangarrary['category'] = "Hair Weaving";
            //$zuzhuangarrary['lotNum'] = $value_p1;
            unset($zuzhuangarrary['laceSize']); 
          }else{
            $zuzhuangarrary[$key_p1] = $value_p1;
          }
          if(empty($zuzhuangarrary['type_country'])){
              $zuzhuangarrary['type_country'] = $country_type_value;
          }
    }
    // print_r($zuzhuangarrary);
     echo "\n";
      $result = Getinsert($arrData=$zuzhuangarrary,$table='dlz_order_product',$db,$sync_data="0");//
          //给定处理过的产品，用 1 标示；
      if($result == '0'){
          $syncstatus_sql = "update dlz_attribute_all_id set sync_status='1' where item_id='$item_id'";
          $db->exec($syncstatus_sql);
    }
   }//


}
$db = null;

//include("OptionPostorderinfo.php");
